//对uni.request简单封装
import BASE from '@/Base'
import {ERROR_NONE,ERROR_TOKEN_INVALID,ERROR_USER_STATUS_INVALID,ERROR_FAIL} from "./error_code";
import stringUtils from "../util/stringUtils.js";

// console.log(BASE.BASE_URL)

function uniRequest(fn) {
    return function (obj = {}) {
        return new Promise((resolve, reject) => {
            obj.success = function (res) {
                uni.hideLoading()
				if(res.data.code == ERROR_TOKEN_INVALID){
					uni.showToast({
					    title: '登录失效，请重新登录',
					    icon: 'none',
					    duration: 1000,
					});
					uni.removeStorageSync('token')
					uni.navigateTo({
					    url: '../login/login',
					});
				} else if(res.data.code == ERROR_USER_STATUS_INVALID){
					uni.showToast({
					    title: res.data.msg,
					    icon: 'none',
					    duration: 1000,
					});
				} else {
					if(res.data.code == ERROR_FAIL){
					    if(res.data.msg.indexOf('token错误')>=0){
							uni.removeStorageSync('token')
					        uni.navigateTo({
					            url: '../login/login',
					        });
					    } else {
					        resolve(res);
					    }

					} else {
					    resolve(res);
					}

				}
            }
            obj.fail = function (res) {
                uni.showToast({title:'no allow_origin',duration: 2000,icon:'loading'})
                uni.removeStorageSync('token')
                setTimeout(function() {
                    uni.navigateTo({
                        url: '../errorPage/error1',
                    })
                }, 3000);
                // window.location.href = '/reg'
                //失败
                reject(res)
            }
            fn(obj)
        })
    }
}

function login() {
    let phone = uni.getStorageSync('phone');
    let password = uni.getStorageSync('password');
    if (stringUtils.isBlank(phone) || stringUtils.isBlank(password)) {
        uni.navigateTo({
            url: '../login/login',
        });
    } else {
        uni.showToast({
            title:'登录状态失效，自动登录中',
            icon: 'none',
            duration: 300,
        });
        uni.request({
            url: BASE.BASE_URL + 'userauth/login',
            data:{
                phone: phone,
                password: password,
            },
            success: (res) => {
                uni.setStorageSync('token', res.data.data.token);
                uni.setStorageSync('userType', res.data.data.user_type);
                uni.setStorageSync('password', password);
                uni.setStorageSync('phone',phone);
                uni.showToast({
                    title:'登录成功',
                    icon: 'none',
                    duration: 300,
                });
                setTimeout(function() {
                    uni.switchTab({
                        url: '../home/home'
                    })
                }, 300);
                uni.hideLoading()
            },
            fail: (res)=>{
                uni.hideLoading()
                uni.showToast({
                    title: '登录失败',
                    icon: 'none',
                    duration: 1000,
                });
                uni.switchTab({
                    url: '../login/login'
                })
            }
        });
    }
}
const getRequest = (url, data) => {
    let getRequest = uniRequest(uni.request)
    return getRequest({
        url: url,
        data: data,
        method: 'get',
        header: {
            'X-Requested-With': 'XMLHttpRequest',
            "Accept": "application/json",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        },
        dataType: 'json'
    })
};
const putRequest = (url, data) => {
    let putRequest = uniRequest(uni.request)
    return putRequest({
        url: url,
        data: data,
        method: 'PUT',
        header: {
            'X-Requested-With': 'XMLHttpRequest',
            "Accept": "application/json",
            "Content-Type": "application/json; charset=UTF-8"
        },
        dataType: 'json'
    })
};
const postRequest = (url, data) => {
    let postRequest = uniRequest(uni.request)
    return postRequest({
        url: url,
        data: data,
        method: 'post',
        header: {
            'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        dataType: 'json'
    })
};
const deleteRequest = (url, data) => {
    let deleteRequest = uniRequest(uni.request)
    return deleteRequest({
        url: url,
        data: data,
        method: 'DELETE',
        header: {
            'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        dataType: 'json'
    })
};
// 传输对象数组
const jsonRequest = (url, data) => {
  let postRequest = uniRequest(uni.request)
  return postRequest({
    url: url,
    data: data,
    method: 'post',
    header: {
      'X-Requested-With': 'XMLHttpRequest',
      "Accept": "application/json",
      'Content-Type': 'application/json; charset=UTF-8'
    },
    dataType: 'json'
  })
};
export default {
  postRequest,
  getRequest,
  putRequest,
  deleteRequest,
  jsonRequest
}
